﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Sample.GameManagement.Contracts;
using Zhaoxi.GameManagement.Entites;
using Zhaoxi.GameManagement.Entites.RequestFeatures;
using Zhaoxi.GameManagement.Entites.ResponseType.Paging;
using Zhaoxi.GameManagement.EntityFrameworkCore.Repositories.Extensions;

namespace Zhaoxi.GameManagement.EntityFrameworkCore.Repositories
{
    public class PlayerRepository : RepositoryBase<Player>, IPlayerRepository
    {
        public PlayerRepository(GameManagementDbContext repositoryContext) : base(repositoryContext)
        {
        }

        public Task<PagedList<Player>> GetPlayers(PlayerParameter parameter)
        {
            return FindByCondition(player => 
                    player.DateCreated.Date >= parameter.MinDateCreated.Date && 
                    player.DateCreated.Date <= parameter.MaxDateCreated.Date)
                .SearchByAccount(parameter.Account)
                .OrderByQuery(parameter.OrderBy)
                .ToPagedList(parameter.PageNumber, parameter.PageSize);
        }

        public ValueTask<Player?> GetPlayerById(Guid playerId)
        {
            return GameDbContext.Players.FindAsync(playerId);
        }

        public Task<Player?> GetPlayerWithCharacters(Guid playerId)
        {
            return FindByCondition(player => player.Id == playerId)
                .Include(player => player.Characters)
                .FirstOrDefaultAsync();
        }

        public void CreatePlayer(Player player)
        {
            Create(player);
        }

        public void UpdatePlayer(Player player)
        {
            Create(player);
        }
    }   
}
